/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.basics.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.rzico.annotation.IgnoreSwaggerParameter;
import com.rzico.core.entity.SysUser;
import io.swagger.models.auth.In;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import io.swagger.annotations.ApiModelProperty;

/**
 * <pre>
 *   会员数据库对象
 * </pre>
 * @author Rzico Boot
 * @version 1.0
 */
@Data
@JsonIgnoreProperties(value = {"handler"})
@Table(name = "wx_member")
public class Member implements Serializable {

    @ApiModelProperty(value = "")
    @Id
    protected Long id;

    @ApiModelProperty(value = "创建日期")
    protected java.util.Date createDate;

    @ApiModelProperty(value = "修改日期")
    protected java.util.Date modifyDate;

    @ApiModelProperty(value = "用户名")
    protected String username;

    @ApiModelProperty(value = "钱包余额")
    @JsonIgnore
    protected java.math.BigDecimal balance;

    @ApiModelProperty(value = "冻结余额")
    @JsonIgnore
    protected java.math.BigDecimal freezeBalance;

    @ApiModelProperty(value = "金币余额")
    protected Long point;

    @ApiModelProperty(value = "兑换积分")
    protected Long matchPoint;

    @ApiModelProperty(value = "冻结金币")
    @JsonIgnore
    protected Long freezePoint;

    @ApiModelProperty(value = "消费金额")
    protected java.math.BigDecimal amount;

    @ApiModelProperty(value = "累计业绩")
    protected java.math.BigDecimal achieve;

    @ApiModelProperty(value = "是否删除")
    protected Boolean deleted;

    @ApiModelProperty(value = "手机号")
    protected String mobile;

    @ApiModelProperty(value = "编码")
    protected String code;

    @ApiModelProperty(value = "备用电话")
    protected String phone;

    @ApiModelProperty(value = "邮箱")
    protected String email;

    @ApiModelProperty(value = "客户备注")
    protected String memo;

    @ApiModelProperty(value = "姓名/公司名")
    protected String name;

    @ApiModelProperty(value = "出生日期")
    protected java.util.Date birth;

    @ApiModelProperty(value = "性别 {0:男,1:女,2:隐藏}")
    protected Integer gender;

    @ApiModelProperty(value = "所在地")
    protected Integer areaId;

    @ApiModelProperty(value = "所在区")
    protected String areaName;

    @ApiModelProperty(value = "地址")
    protected String address;

    @ApiModelProperty(value = "推广会员")
    @Column(name = "promoter")
    protected Long promoterId;

    @ApiModelProperty(value = "所属分类")
    protected Long memberCategoryId;

    @ApiModelProperty(value = "所属企业")
    protected Long companyId;

    @ApiModelProperty(value = "等级{vip0..vip5}")
    protected Integer vip;

    @ApiModelProperty(value = "商户锁定时间")
    @JsonIgnore
    protected java.util.Date lockMchDate;

    @ApiModelProperty(value = "vip有效期")
    protected java.util.Date vipExpired;

    @ApiModelProperty(value = "所属商户")
    @JsonIgnore
    protected String mchId;

    @ApiModelProperty(value = "所属门店")
    protected Long shopId;

    @ApiModelProperty(value = "客户类型:0.个人,1.企业")
    protected Integer memberType;

    @ApiModelProperty(value = "开票类型:0.不开票,1.增票,2.普票")
    protected Integer taxType;

    @ApiModelProperty(value = "纳税识别号")
    protected String taxNum;

    @ApiModelProperty(value = "所属部门")
    protected String officeId;

    @ApiModelProperty(value = "付款方式(cashPayPlugin:现金客户,debtPayPlugin:月结客户)")
    protected String paymentPluginId;

    @ApiModelProperty(value = "绑定用户")
    @JsonIgnore
    protected String userId;

    @ApiModelProperty(value = "归属企业")
    @JsonIgnore
    protected Long enterpriseId;

    @ApiModelProperty(value = "创建者")
    @JsonIgnore
    protected String createBy;

    @ApiModelProperty(value = "更新者")
    @JsonIgnore
    protected String modifyBy;

    @ApiModelProperty(value = "会员分类", hidden = true)
    @JsonIgnore
    protected MemberCategory memberCategory;

    /**
     * 当前对象的量表信息
     */
    @JsonIgnore
    @IgnoreSwaggerParameter
    protected SysUser sysUser;

    /**
     * 当前对象的量表信息
     */
    @JsonIgnore
    @IgnoreSwaggerParameter
    protected Company company;

    /**
     * 当前对象的量表信息
     */
    @JsonIgnore
    @IgnoreSwaggerParameter
    protected Shop shop;

    @JsonIgnore
    @IgnoreSwaggerParameter
    protected List<Vip> vipList = Collections.emptyList();

    @JsonIgnore
    @IgnoreSwaggerParameter
    protected List<Card> cardList = Collections.emptyList();

    public Long calculateAvailablePoint() {
        Long point = getPoint()-getFreezePoint();
        if (point<0) {
            return 0L;
        }
        return point;
    }


    public BigDecimal calculateAvailableBalance() {
        BigDecimal balance = getBalance().subtract(getFreezeBalance());
        if (balance.compareTo(BigDecimal.ZERO)<0) {
            return BigDecimal.ZERO;
        }
        return balance;
    }

    @ApiModelProperty(value = "名称", hidden = true)
    public String getMemberCategoryName() {
        MemberCategory member = getMemberCategory();
        if (null != member){
            return member.getName();
        }else{
            return "";
        }
    }

    public void setMemberCategoryName(String memberCategoryName) {

    }

    @ApiModelProperty(value = "企业名称", hidden = true)
    public String getCompanyName() {
        Company company = getCompany();
        if (null != company){
            return company.getName();
        }else{
            return "";
        }
    }

    public void setCompanyName(String companyName) {

    }

    @ApiModelProperty(value = "头像", hidden = true)
    public String getAvatar() {
        SysUser sysUser = getSysUser();
        if (null != sysUser){
            return sysUser.getAvatar();
        }else{
            return "";
        }
    }

    public void setAvatar(String avatar) {

    }

    @ApiModelProperty(value = "卡余额", hidden = true)
    public BigDecimal getCardBalance() {
        List<Card> cardList = getCardList();
        BigDecimal amount = BigDecimal.ZERO;
        for (Card card:cardList) {
            amount = amount.add(card.getBalance());
        }
        return amount;
    }

    public void setCardBalance(BigDecimal value) {

    }

    public Integer getVvip() {
        Integer vip = getVip();
        if (getVipExpired()!=null && getVipExpired().after(new Date())) {
            List<Vip> vips = getVipList();
            for (Vip v:vips) {
                if (v.getMethod().equals(3)) {
                    vip = v.getVip();
                }
            }
        }
        if (vip<getVip()) {
            vip = getVip();
        }
        return vip;
    }


    public void setVvip(Integer value) {

    }

    public Vip vipObject() {
        List<Vip> vips = getVipList();
        Integer vip = getVvip();
        for (Vip v:vips) {
            if (v.getVip().equals(vip)) {
                return v;
            }
        }
        return null;
    }

    public String getVipName() {
        List<Vip> vips = getVipList();
        Integer vip = getVvip();
        for (Vip v:vips) {
            if (v.getVip().equals(vip)) {
                return v.getName();
            }
        }
        return "VIP"+String.valueOf(vip);
    }

    public void setVipName(String vipName) {

    }

    public String getShopName() {
        Shop shop = getShop();
        if (shop!=null) {
            return shop.getName();
        }

        return null;
    }

    public void setShopName(String shopName) {

    }


}
